package com.meida.module.bus.provider.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.springmvc.base.BaseController;
import com.meida.module.bus.client.entity.BusReceiptDeliveryRecord;
import com.meida.module.bus.client.entity.BusReceiptDeliveryRecordBack;
import com.meida.module.bus.provider.api.util.ApiConstants;
import com.meida.module.bus.provider.api.util.PmsApiUtil;
import com.meida.module.bus.provider.service.BusRecDeliBkReqConfService;
import com.meida.module.bus.provider.service.BusRecDeliReqConfService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 材料员领料冲销申请确认表
 *
 * @author flyme
 * @date 2020-12-02
 */
@RestController
@RequestMapping("/bus/recDeliBkReqConf/")
public class BusRecDeliBkReqConfController extends BaseController<BusRecDeliBkReqConfService, BusReceiptDeliveryRecordBack>  {
    @Autowired
    private PmsApiUtil pmsApiUtil;
    @Autowired
    private BusRecDeliReqConfService busRecDeliReqConfService;

    @ApiOperation(value = "预留发货冲销申请表-分页列表", notes = "预留发货冲销申请表分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }

    @ApiOperation(value = "预留发货冲销申请表-列表", notes = "预留发货冲销申请表列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "预留发货冲销申请表-添加", notes = "添加预留发货冲销申请表")
    @PostMapping(value = "save")
    public ResultBody save(@RequestBody Map params) throws Exception {
        StringBuffer errDetail = new StringBuffer();
        String postDateStr = String.valueOf(params.get("postDate"));
        String cerDateStr = String.valueOf(params.get("cerDate"));
        String cerTxtStr = String.valueOf(params.get("cerTxt"));
        if (StrUtil.isEmpty(postDateStr) || StrUtil.isEmpty(cerDateStr) || StrUtil.isEmpty(cerTxtStr)) {
            if (StrUtil.isEmpty(postDateStr)) {
                errDetail.append("抬头文本为空");
            }
            if (StrUtil.isEmpty(cerDateStr)) {
                errDetail.append("，过账日期为空");
            }
            if (StrUtil.isEmpty(cerTxtStr)) {
                errDetail.append("，凭证日期为空");
            }
            errDetail.append(errDetail.toString());
            return ResultBody.failed(errDetail.toString());
        }
        List<String> selectIdsStri = (List<String>) params.get("selectIds");
        List<Long> selectIdsList = Lists.newArrayList();
        selectIdsStri.stream().forEach(idO -> {
            selectIdsList.add(Long.valueOf(idO));
        });
        List<BusReceiptDeliveryRecordBack> busReceiptDeliveryRecordBacks = (List<BusReceiptDeliveryRecordBack>) bizService.listByIds(selectIdsList);
        BusReceiptDeliveryRecordBack header = busReceiptDeliveryRecordBacks.get(0);
        Integer ver = pmsApiUtil.getLastVer(header.getProjectId(), 0) ? ApiConstants.VER_100 : ApiConstants.VER_200;
        busReceiptDeliveryRecordBacks.stream().forEach(updObj -> {
            updObj.setBudat(DateUtil.parseDate(postDateStr));
            updObj.setBldat(DateUtil.parseDate(cerDateStr));
            updObj.setBktxt(cerTxtStr);
            updObj.setSyncStatus(ApiConstants.REC_DELIVERY_FINISH);
            updObj.setDelFlg(ApiConstants.DEL_OFF);
            updObj.setVer(String.valueOf(ver));
        });
        bizService.syncSap(busReceiptDeliveryRecordBacks);
        List<BusReceiptDeliveryRecord> reqUpds = Lists.newArrayList();
        List<BusReceiptDeliveryRecordBack> bkReqUpds = Lists.newArrayList();
        busReceiptDeliveryRecordBacks.stream().forEach(bkObj->{
            if(!ApiConstants.REC_DELIVERY_ERR.equals(bkObj.getSyncStatus())) {
                BusReceiptDeliveryRecord busReceiptDeliveryRecord = busRecDeliReqConfService.getById(bkObj.getRecParentId());
                // 可冲销数量
                busReceiptDeliveryRecord.setCharOffCountCan(NumberUtil.sub(busReceiptDeliveryRecord.getCharOffCountCan(), bkObj.getCharOffCount()));
                reqUpds.add(busReceiptDeliveryRecord);
                bkReqUpds.add(bkObj);
            }
        });
        if(0 < reqUpds.size()) {
            busRecDeliReqConfService.updateBatchById(reqUpds);
        }
        if(0 < bkReqUpds.size()) {
            bizService.updateBatchById(bkReqUpds);
        }

        return ResultBody.msg("提交成功");
    }

    @ApiOperation(value = "预留发货冲销申请表-更新", notes = "更新预留发货冲销申请表")
    @PostMapping(value = "update")
    public ResultBody update(@RequestBody(required = false) List<BusReceiptDeliveryRecordBack> records) {
        bizService.updateBatchById(records);
        return ResultBody.ok();
    }

    @ApiOperation(value = "预留发货冲销申请表-删除", notes = "删除预留发货冲销申请表")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return bizService.delete(params);
    }


    @ApiOperation(value = "预留发货冲销申请表-详情", notes = "预留发货冲销申请表详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }
    /**
    * 导出预留发货冲销申请表统计
    *
    * @param params
    * @param request
    * @param response
    */
    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        bizService.export(params, request, response, "预留发货冲销申请表统计", "预留发货冲销申请表统计表");
    }

    @ApiOperation(value = "预留发货记录表-请求驳回", notes = "请求驳回")
    @PostMapping(value = "reqBkCancel")
    public ResultBody reqBkCancel(@RequestBody Long selectIds[]) {
        bizService.reqBkCancel(selectIds);
        return ResultBody.msg("提交成功");
    }

}
